import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

import javax.imageio.ImageIO;
//draw map image and write to the FMap.txt and print selected point

public class MultipleWalks {
	static ImageProcessing imp = new ImageProcessing(3);
	static BufferedImage image;

	static RandomWalkSquare walk1 = new RandomWalkSquare(10,100,25,imp);
	static RandomWalkSquare walk2 = new RandomWalkSquare(10,100,25,imp);
	static Point[] path1;
	static Point[] path2;
	
	public MultipleWalks(){}
	public static void main(String[] args) throws IOException{
		
		Color colorToFind = new Color(254,0,0);	
		Vector<Point> pixels = imp.pixelSearch(colorToFind,imp.readImage());	
		int[][] maps = imp.convertToMap();
		int[][] FMap = new int[maps.length][maps[0].length];
		
		//writeCSV(maps);
		Point point1 = generateStartPoint(pixels);
		Point point2 = generateStartPoint(pixels);
		
		Person walker1 = new Person(point1.x,point1.y,10);
		path1 = walk1.walkRandomly(walker1, maps);
		
		Person walker2 = new Person(point2.x,point2.y,10);
		path2 = walk2.walkRandomly(walker2, maps);
        
		image=imp.image;
		writeImage(image,maps);
		
		addToFMap(path1,FMap);
		addToFMap(path2,FMap);
		
		drawFMap(FMap);
		selectPoint(FMap);
	
    }
	public static Point generateStartPoint(Vector<Point> pixels){
		int size = pixels.size();
		Random generator = new Random();
		int index = generator.nextInt(size);
		return pixels.get(index);
		
	}
	public static void writeCSV(int[][] maps) throws IOException{
		File file = new File( "C:/Users/mengdi/Desktop/material/2.csv");  
        FileWriter writer = new FileWriter(file);
        for(int i=0;i<maps.length;i++){
        	for(int j=0;j<maps[0].length;j++){
        		if(maps[i][j]==1) writer.append(i+","+j+"\n");
        	}
        }
        writer.flush();
        writer.close();
	}
	public static void selectPoint(int[][] FMap){
		for(int i=0;i<FMap.length;i++){
			for(int j=0;j<FMap[0].length;j++){
				if(FMap[i][j]!=0) 
					System.out.println("("+i+","+j+")"+" Frequency:"+FMap[i][j]);
			}
		}
	}
	public static void addToFMap(Point[] path, int[][] FMap){
		int size = path.length;
		for(int i= 0;i<size;i++){
			int x = path[i].x;
			int y = path[i].y;
			FMap[x][y]++;
		}
		
	}
	public static void drawFMap(int[][] FMap) throws IOException{
		File file = new File( "C:/Users/mengdi/Desktop/material/Fmap.txt");  
        //if ( !file.exists() ) file.createNewFile();
    	FileWriter writer = new FileWriter(file);
    	for(int i=0;i<FMap.length;i++){
			for(int j=0;j<FMap[i].length;j++){
				writer.append(FMap[i][j]+" ");
			}
			writer.append("\n");
		}
    	    writer.flush();
    	    writer.close();
	}
	public static BufferedImage setImage(BufferedImage image, int[][] map){
		int w = image.getWidth();
        int h = image.getHeight(); 
		BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Color red = new Color(254,0,0);
		int redColor = red.getRGB();
		for(int i=0;i<map.length;i++){
			for(int j=0;j<map[0].length;j++){
				if(map[i][j]==1) bufferedImage.setRGB(i, j, redColor);
			}
		}
		return bufferedImage;
	}
	public static BufferedImage setPath(BufferedImage image,Point[] path, int pathColor){
		for(int i=0;i<path.length;i++){
			int x=path[i].x;
			int y=path[i].y;
			for(int m=0;m<10;m++){
				for(int n=0;n<10;n++){
					image.setRGB(x+m,y+n,pathColor);
				}
			}
		}
		return image;
	}
	public static void writeImage(BufferedImage image, int[][] map) throws IOException{
		BufferedImage bufferedImage = setImage(image,map);
				
		Color green = new Color(0,255,0);
		int pathColor1 = green.getRGB();
		bufferedImage = setPath(bufferedImage,path1,pathColor1);		
		
		Color white = new Color(255,255,255);
		int pathColor2 = white.getRGB();
		
		bufferedImage = setPath(bufferedImage,path2,pathColor2);
		
		String filename = "2 walks on level 3 try 2";
		File file = new File("C:/Users/mengdi/Desktop/material/"+filename+".jpg");
		
		ImageIO.write(bufferedImage,"jpg",file);
		System.out.println(file.getName());
				
	}

}
